import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Deque;
public class Main {
    public static void main(String[] args) {
        Queue<String> q = new LinkedList<>();
        // 添加3个元素到队列:
        q.offer("apple");
        q.offer("pear");
        q.offer("banana");
        //从队列中查看头元素
        System.out.println(q.peek()); // apple
        // 从队列取出元素:
        System.out.println(q.poll()); // apple
        System.out.println(q.poll()); // pear
        System.out.println(q.poll()); // banana
        System.out.println(q.poll()); // null,因为队列是空的



        Queue<String> q2 = new PriorityQueue<>();
        // 添加3个元素到队列:
        q2.offer("apple");
        q2.offer("pear");
        q2.offer("banana");
        System.out.println(q2.poll()); // apple
        System.out.println(q2.poll()); // banana
        System.out.println(q2.poll()); // pear
        System.out.println(q2.poll()); // null,因为队列为空

        //加权queue
        Queue<User> q3 = new PriorityQueue<>(new UserComparator());
        // 添加3个元素到队列:
        q3.offer(new User("Bob", "A1"));
        q3.offer(new User("Alice", "A2"));
        q3.offer(new User("Boss", "V1"));
        System.out.println(q3.poll()); // Boss/V1
        System.out.println(q3.poll()); // Bob/A1
        System.out.println(q3.poll()); // Alice/A2
        System.out.println(q3.poll()); // null,因为队列为空

        //双端queue dequeue
        Deque<String> deque = new LinkedList<>();
        deque.offerLast("A"); // A
        deque.offerLast("B"); // A <- B
        deque.offerFirst("C"); // C <- A <- B
        System.out.println(deque.pollFirst()); // C, 剩下A <- B
        System.out.println(deque.pollLast()); // B, 剩下A
        System.out.println(deque.pollFirst()); // A
        System.out.println(deque.pollFirst()); // null
    }
}
class UserComparator implements Comparator<User> {
    public int compare(User u1, User u2) {
        if (u1.number.charAt(0) == u2.number.charAt(0)) {
            // 如果两人的号都是A开头或者都是V开头,比较号的大小:
            return u1.number.compareTo(u2.number);
        }
        if (u1.number.charAt(0) == 'V') {
            // u1的号码是V开头,优先级高:
            return -1;
        } else {
            return 1;
        }
    }
}

class User {
    public final String name;
    public final String number;

    public User(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String toString() {
        return name + "/" + number;
    }
}